package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.annotaion.SystemControllerLog;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Menu;
import com.itheima.pojo.User;
import com.itheima.service.UserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.LinkedHashSet;
import java.util.List;

/**
 * 用户操作
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Reference
    private UserService userService;

    //获得当前登录用户的用户名
    @RequestMapping("/getUsername")
    @SystemControllerLog(MessageConstant.GET_USERNAME_FAIL)
    public Result getUsername() {
        //当Spring security完成认证后，会将当前用户信息保存到框架提供的上下文对象
        org.springframework.security.core.userdetails.User user = (org.springframework.security.core.userdetails.User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if (user != null) {
            String username = user.getUsername();
            return new Result(true, MessageConstant.GET_USERNAME_SUCCESS, username);
        }

        return new Result(false, MessageConstant.GET_USERNAME_FAIL);
    }

    @RequestMapping("/findMenuList")
    @SystemControllerLog(MessageConstant.GET_MENU_FAIL)
    public Result findMenuList() {
        //获取当前用户user对象
        org.springframework.security.core.userdetails.User user = (org.springframework.security.core.userdetails.User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if (user != null) {
            String username = user.getUsername();
            //调用service层根据用户名查询菜单列表
            LinkedHashSet<Menu> menus = userService.findMenuListByUsername(username);
            return new Result(true, MessageConstant.GET_MENU_SUCCESS, menus);
        }

        return new Result(false, MessageConstant.GET_MENU_FAIL);
    }

    //新增检查组
    @RequestMapping("/add")
    @SystemControllerLog("新增失败")
    public Result add(@RequestBody User user, Integer[] roleIds) {
        userService.add(user, roleIds);
        return new Result(true, "新增成功");//新增成功
    }

    //分页查询
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        return userService.pageQuery(queryPageBean);
    }

    //根据ID查询检查组
    @RequestMapping("/findById")
    @SystemControllerLog("查询失败")
    public Result findById(Integer id) {
        User user = userService.findById(id);
        return new Result(true, "查询成功", user);//查询成功
    }

    //根据检查组ID查询检查组包含的多个检查项ID
    @RequestMapping("/findRoleIdsByUserId")
    @SystemControllerLog("查询失败")
    public Result findRoleIdsByUserId(Integer id) {
        List<Integer> roleIds = userService.findRoleIdsByUserId(id);
        return new Result(true, "查询成功", roleIds);//查询成功
    }

    //编辑检查组
    @RequestMapping("/edit")
    @SystemControllerLog("修改失败")
    public Result edit(@RequestBody User user, Integer[] roleIds) {
        userService.edit(user, roleIds);
        return new Result(true, "修改成功");//修改成功
    }

    //查询所有检查组
    @RequestMapping("/findAll")
    @SystemControllerLog("查询失败")
    public Result findAll() {
        List<User> list = userService.findAll();
        return new Result(true, "查询成功", list);//查询成功
    }

    @PreAuthorize("hasAuthority('USER_DELETE')")//权限校验
    @RequestMapping("/delete")
    @SystemControllerLog("删除用户失败")
    public Result delete(Integer id) {
        userService.deleteById(id);
        return new Result(true, "删除用户成功");
    }
}
